﻿using LoadClient.YUAN.C;
using LoadClient.YUAN.E;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LoadClient.YUAN.F
{
    public partial class FrmLoadRecord : Form
    {
        public FrmLoadRecord()
        {
            InitializeComponent();
        }

        private void FrmLoadRecord_Load(object sender, EventArgs e)
        {
            LanguageHelper.SetControlLanguageText(this);
            this.Text = LanguageHelper.SetFormlLanguageText(this.Text);

            BTN_TodaySubmit_Click(null, null);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        string custmerformat = "yyyy-MM-dd HH:mm:ss";

        List<LoadReport> listlr = new List<LoadReport>();

        private void BTN_SUBMIT_Click(object sender, EventArgs e)
        {
            DGV_LoadRecord.Rows.Clear();
            listlr = ApiManager.GetLoadReportListByDate(Start.Value, End.Value);
            listlr.ForEach(a => DGV_LoadRecord.Rows.Add(new object[] { a.Id, "Load " + a.StationNumber, a.ProductSerial, a.FbarId, a.UID, a.ExeCarID, a.ProductId, a.ProductName, a.ProductPPid, a.Quantity, a.InsertTime.ToString(custmerformat) }));
        }

        private void BTN_TodaySubmit_Click(object sender, EventArgs e)
        {
            DGV_LoadRecord.Rows.Clear(); Start.Value = DateTime.Today;
            listlr = ApiManager.GetLoadReportListByDate(Start.Value, End.Value);
            listlr.ForEach(a => DGV_LoadRecord.Rows.Add(new object[] { a.Id, "Load " + a.StationNumber, a.ProductSerial, a.FbarId, a.UID, a.ExeCarID, a.ProductId, a.ProductName, a.ProductPPid, a.Quantity, a.InsertTime.ToString(custmerformat) }));
        }

        private void BTN_allSubmit_Click(object sender, EventArgs e)
        {
            DGV_LoadRecord.Rows.Clear();
            listlr = ApiManager.GetLoadReportList();
            listlr.ForEach(a => DGV_LoadRecord.Rows.Add(new object[] { a.Id, "Load " + a.StationNumber, a.ProductSerial, a.FbarId, a.UID, a.ExeCarID, a.ProductId, a.ProductName, a.ProductPPid, a.Quantity, a.InsertTime.ToString(custmerformat) }));
        }

        private void BTN_SerialID_Click(object sender, EventArgs e)
        {
            if (TB_SerialID.Text.Equals(string.Empty) && TB_FbarID.Text.Equals(string.Empty)) return;
            //通过FbarID查询
            if (TB_SerialID.Text.Equals(string.Empty) && !TB_FbarID.Text.Equals(string.Empty))
            {
                int fbarid = Convert.ToInt32(TB_FbarID.Text); if (fbarid == 0) return;
                DGV_LoadRecord.Rows.Clear();
                listlr = ApiManager.GetLoadReportListByFbarID(fbarid);
                listlr.ForEach(a => DGV_LoadRecord.Rows.Add(new object[] { a.Id, "Load " + a.StationNumber, a.ProductSerial, a.FbarId, a.UID, a.ExeCarID, a.ProductId, a.ProductName, a.ProductPPid, a.Quantity, a.InsertTime.ToString(custmerformat) }));
            }
            if (!TB_SerialID.Text.Equals(string.Empty))
            {
                int serial = Convert.ToInt32(TB_SerialID.Text); if (serial == 0) return;
                DGV_LoadRecord.Rows.Clear();
                listlr = ApiManager.GetLoadReportListBySerial(serial);
                listlr.ForEach(a => DGV_LoadRecord.Rows.Add(new object[] { a.Id, "Load " + a.StationNumber, a.ProductSerial, a.FbarId, a.UID, a.ExeCarID, a.ProductId, a.ProductName, a.ProductPPid, a.Quantity, a.InsertTime.ToString(custmerformat) }));
            }
        }

        private void BTN_Export_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Please select a path"; sfd.FileName = "Load Notebook" + COP.GetGuid();
                sfd.Filter = "Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    MiniExcel.SaveAsByTemplate(sfd.FileName, "LoadRecordTemplate.xlsx", GetExportContent());
                    MessageBox.Show("Export Compelete!", "Operation tips", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("export progress Error!", "Operation tips", MessageBoxButtons.OK);
            }
        }

        private object GetExportContent()
        {
            var data = new
            {
                List = LrToLrEX(listlr),
            };
            return data;
        }

        private List<LoadReportEx> LrToLrEX(List<LoadReport> lrs)
        {
            List<LoadReportEx> lrex = new List<LoadReportEx>();
            for (int i = 0; i < lrs.Count; i++)
            {
                lrex.Add(new LoadReportEx(lrs[i].Id, lrs[i].StationNumber.ToString(), lrs[i].ProductSerial, lrs[i].FbarId, lrs[i].ProductId, lrs[i].ProductName, lrs[i].ProductPPid, lrs[i].Quantity, lrs[i].UnQuantity, lrs[i].Operator, lrs[i].InsertTime.ToString(custmerformat)));
            }
            return lrex;
        }
    }
}
